﻿using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

namespace Sidvall.Reflection.Uwp
{
    public class Code : ICode
    {
        #region Public Members

        #region GetAssemblyAsync

        public Task<Assembly> GetAssemblyAsync(byte[] rawAssembly)
        {
            throw new System.NotSupportedException();
        }

        #endregion
        #region GetAssemblyTypeNames

        public string[] GetAssemblyTypeNames(Assembly assembly, bool? isClass, bool? isAbstract, bool? isPublic, TypeInfo baseType)
        {
            if (assembly == null)
                return null;
            var names = new List<string>();
            foreach (var type in assembly.DefinedTypes)
            {
                if ((isClass != null) && (isClass != type.IsClass))
                    continue;
                if ((isAbstract != null) && (isAbstract != type.IsAbstract))
                    continue;
                if ((isPublic != null) && (isPublic != type.IsPublic))
                    continue;
                if ((baseType != null) && (!baseType.IsAssignableFrom(type)))
                    continue;
                names.Add(type.FullName);
            }
            return names.ToArray();
        }

        #endregion
        #region GetAssembliesAsync

        public Task<IEnumerable<Assembly>> GetAssembliesAsync()
        {
            throw new System.NotSupportedException();
        }

        #endregion
        #region GetFields

        public IEnumerable<FieldInfo> GetFields(System.Type type, bool? isInterface, bool? isAbstract, bool? isPublic, string name)
        {
            if (type == null)
                return null;
            return (from o in type.GetFields()
                    where ((string.IsNullOrWhiteSpace(name)) || (o.Name == name))
                    select o);
        }

        #endregion
        #region GetProperties

        public IEnumerable<PropertyInfo> GetProperties(System.Type type, bool? isInterface, bool? isAbstract, bool? isPublic, string name)
        {
            if (type == null)
                return null;
            return (from o in type.GetProperties()
                    where ((string.IsNullOrWhiteSpace(name)) || (o.Name == name))
                    select o);
        }

        #endregion

        #region ToString

        public override string ToString()
        {
            return "Code - UWP";
        }

        #endregion

        #endregion
        #region Constructors

        public Code()
        {
        }

        #endregion
    }
}
